<?php

class Parameter extends Parameter_model {

    function __construct() {
        parent::__construct();
    }

    public static function initParam() {
        $param = new Parameter();
        $param->orderBy('id');
        $param->find();
        while($param->fetchNext()) {
            if (!defined($param->code)) {
                define($param->code, $param->value);
            }
        }
    }
    
    function getList($filter = array()) {
        $parameter = new Parameter();
        if(isset($filter['name']) && $filter['name']) {
            $parameter->addWhere("parameter.name LIKE '%".$filter['name']."%'");
        }
        if(isset($filter['type']) && $filter['type']) {
            $parameter->addWhere("parameter.type = ".intval($filter['type']));
        }
        
        if(isset($filter['category']) && $filter['category']) {
            $parameter->addWhere("parameter.category = ".intval($filter['category']));
        }
        
        $parameter->orderBy("parameter.name");
        $parameter->find();
                
        return $parameter;            
    }
    
    function validateInput() {

        if (empty ($this->name)) {
            MessageHandler::add (_MSG_PARAM_NAME_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        }
        
        if (strlen($this->name) > MAX_LENGTH_NAME) {
            MessageHandler::add (_MSG_PARAM_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
        }
        
        if (self::isExistsByName($this->id, $this->name)) {
            MessageHandler::add (_MSG_PARAM_NAME_ALREADY_EXIST, MSG_ERROR, LOG_ONLY);
        }
        
        if (empty ($this->code)) {
            MessageHandler::add (_MSG_PARAM_CODE_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        }
        
        if (strlen($this->code) > MAX_LENGTH_NAME) {
            MessageHandler::add (_MSG_PARAM_CODE_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
        }
        
        if (self::isExistsByCode($this->id, $this->code)) {
            MessageHandler::add (_MSG_PARAM_CODE_ALREADY_EXIST, MSG_ERROR, LOG_ONLY);
        }
     
        if (empty ($this->value)) {
            MessageHandler::add (_MSG_PARAM_VALUE_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        }
            
        if(!preg_match("/([A-Za-z0-9_])+/", $this->value)){
            MessageHandler::add (_MSG_PARAM_VALUE_INVALID, MSG_ERROR, LOG_ONLY);
        }
        return MessageHandler::countError() > 0 ? false : true;

    }
    
    public function insert() {
        $this->created_date = date('y-m-d H:i:s');
        parent::insert();
    }
    
    public function isExistsByName($id = 0, $name = '')
	{
		if ($name == '')
		{
			return false;
		}	
		$parameter = new Parameter();
		
                if ($id) {
			$parameter->addWhere("id <> {$id}");
		}
                
        $parameter->addWhere("name = '$name'");
		$parameter->find();
		return $parameter->countRows() > 0;
	}
    
    public function isExistsByCode($id = 0, $code = '')
	{
		if ($code == '')
		{
			return false;
		}	
		$parameter = new Parameter();
		
        if ($id) {
			$parameter->addWhere("id <> {$id}");
		}
                
        $parameter->addWhere("code = '$code'");
		$parameter->find();
		return $parameter->countRows() > 0;
	}
    
    function saveValue($id_param, $value, $label=NULL)
	{
        if ($this->session->userdata('id_group') == ADMIN)
		{
            $parameter = new Parameter();
			$parameter->get($id_param);
            if ($parameter->value != $value)
			{
            	$parameter->value = $value;
				$parameter->label = $label;
                if($parameter->code == 'TIMEZONE') {
                    $parameter->value = $label;
                }
                
				if ($parameter->update()){
                    return '1';
                }
            }
		}
		return '0';
	}
}
